package com.company.jobs;

import com.company.constant.RedisConstant;
import com.company.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Random;
import java.util.Set;

/**
 * @author fu xiao liu
 * @date 2021-08-09 20:28
 */

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;


    public void  clearImg() {
        System.out.println("PrintWordsJob start at : " + LocalDate.now() + " " + LocalTime.now() + " , prints:Hello Job- "
                + new Random().nextInt(100));
        //根据Redis 中保存 的两个set 集合进行差值计算,获取图片名称
        System.out.println();
        Set<String> set = jedisPool.getResource()
                .sdiff(RedisConstant.SETMEAL_PIC_RESOURCES,RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (set != null){
            for (String picName : set) {
                //删除七牛云服务器上的图
                // 片
                QiniuUtils.deleteFileFromQiniu(picName);
                //从redis集合中删除图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);
                System.out.println("清理了垃圾图片:" + picName );
            }
        }
    }

}
